/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.redot.constant.BusinessType;
import com.yunqian.redot.domain.Contents;
import com.yunqian.redot.domain.Courses;
import com.yunqian.redot.domain.dto.CourseDto;
import com.yunqian.redot.service.ContentsService;
import com.yunqian.redot.service.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * Describe: 课程管理控制器
 *
 * @author zsy
 * 2018-09-29 15:54:02
 */
 @RestController
 @RequestMapping("/api/courses")
public class CoursesController {

     @Autowired
     private CoursesService coursesService;
     @Autowired
     private ContentsService contentsService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Courses> pagination(@RequestBody Map<String, Object> params) {
        // 更新课程状态
        coursesService.updateStatusByTime();
        return coursesService.pagination(params);
    }

    /**
     * 根据id获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public CourseDto detail(@PathVariable("id") Long id) {
        Courses courses = coursesService.findStrictOne(id);
        List<Contents> contentsList = contentsService.findAllByRelateBusinessAndRelateId(BusinessType.COURSES.getCode(), courses.getId());
        CourseDto dto = new CourseDto();
        dto.setCourses(courses);
        dto.setContentsList(contentsList);
        return dto;
    }

}
